package com.lianxian.xindong.mvp.presenter;

import android.app.Application;
import android.view.View;

import androidx.viewpager.widget.ViewPager;

import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.hxb.library.base.BaseFragment;
import com.hxb.library.base.MessageEvent;
import com.hxb.library.integration.AppManager;
import com.hxb.library.di.scope.ActivityScope;
import com.hxb.library.integration.EventBusManager;
import com.hxb.library.mvp.BasePresenter;
import com.hxb.library.http.imageloader.ImageLoader;

import me.jessyan.rxerrorhandler.core.RxErrorHandler;

import javax.inject.Inject;

import com.hxb.library.utils.LogUtils;
import com.lianxian.xindong.R;
import com.lianxian.xindong.commonres.entity.TabDataBean;
import com.lianxian.xindong.commonsdk.core.EventBusHub;
import com.lianxian.xindong.commonsdk.utils.ImmersionBarUtil;
import com.lianxian.xindong.commonservice.home.bean.HomeMainTableFragmentInfo;
import com.lianxian.xindong.commonservice.me.bean.MeMainTableFragmentInfo;
import com.lianxian.xindong.mvp.contract.MainContract;

import java.util.ArrayList;


/**
 * @作者：hexingbo
 * @时间：06/30/2023
 * @描述：
 */
@ActivityScope
public class MainPresenter extends BasePresenter<MainContract.Model, MainContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;
    @Inject
    ArrayList<CustomTabEntity> mTabEntities;
    @Inject
    ArrayList<BaseFragment> mFragments;


    @Inject
    public MainPresenter(MainContract.Model model, MainContract.View rootView) {
        super(model, rootView);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
        this.mTabEntities = null;
        this.mFragments = null;
    }

    /**
     * 加载页面布局
     */
    public void loadView() {
        getHomeMainTableFragment();
        getMeMainTableFragment();
        if (mTabEntities.size() > 0) {
            mRootView.setContentViewData(mTabEntities, mFragments);
        }
    }

    /**
     * 我的
     */
    private void getMeMainTableFragment() {
        //当非集成调试阶段, 宿主 App 由于没有依赖其他组件, 所以使用不了对应组件提供的服务
        if (mRootView.getMeMainTableService() == null) {
            return;
        }
        MeMainTableFragmentInfo fragment = mRootView.getMeMainTableService().getMeMainTableFragment();
        if (fragment == null) return;
        mTabEntities.add(new TabDataBean(fragment.getName(), fragment.getIconSelectId(), fragment.getIconUnselectId()));
        mFragments.add((BaseFragment) fragment.getTableFragment());
    }

    /**
     * 首页
     */
    private void getHomeMainTableFragment() {
        //当非集成调试阶段, 宿主 App 由于没有依赖其他组件, 所以使用不了对应组件提供的服务
        if (mRootView.getHomeMainTableService() == null) {
            return;
        }
        HomeMainTableFragmentInfo fragment = mRootView.getHomeMainTableService().getHomeMainTableFragment();
        if (fragment == null) return;
        mTabEntities.add(new TabDataBean(fragment.getName(), fragment.getIconSelectId(), fragment.getIconUnselectId()));
        mFragments.add((BaseFragment) fragment.getTableFragment());
    }

    /**
     * 底部菜单点击事件
     *
     * @return
     */
    public OnTabSelectListener getOnTabSelectListener() {
        return new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                mRootView.setPageSelected(position);
            }

            @Override
            public void onTabReselect(int position) {

            }
        };
    }

    /**
     * 中间页面切换
     *
     * @return
     */
    public ViewPager.OnPageChangeListener getOnPageChangeListener() {
        return new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                changeBarColor(position);
                mRootView.setCurrentTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        };
    }

    /**
     * 改变标题栏颜色
     *
     * @param position
     */
    public void changeBarColor(int position) {
        LogUtils.debugInfo("当前tabl:" + position);
        if (position != 0) {
            ImmersionBarUtil.setImmersionBar(mRootView.getContext(), true);
        } else {
            EventBusManager.getInstance().post(new MessageEvent(EventBusHub.EVENT_HOMEFRAGMENT_CHANGEBARCOLOR));
        }
    }

}
